; version in the book
(defun compress (x)
	(if (consp x)
		(compr (car x) 1 (cdr x))
		x))

(defun compr (e n lst)
	(if (null lst)
		(list (n-elts e n))
		(let ((next (car lst)))
			(if (eql next e)
				(compr e (+ n 1) (cdr lst))
				(cons (n-elts e n)
							(compr next 1 (cdr lst)))))))

(defun n-elts (e n)
	(if (> n 1)
		(list n e)
		e))

(setf a '(1 1 1 0 1 0 0 0 0 0))
(format t "~A compressed is:~%~A~%" a (compress a))

; version withtout recursion
(defun my-compr (lst)
	(if (consp lst)
		(let ((newlst nil) (e (car lst)) (n 0))
			(dolist (obj lst)
				(if (eql obj e)
					(incf n 1)
					(if (> n 1)
						(setf newlst (cons (list n e) newlst)
									n 1 
									e obj)
						(setf newlst (cons e newlst)
									n 1
									e obj))))
			(if (> n 1)
				(setf newlst (cons (list n e) newlst))
				(setf newlst (cons e newlst)))
			(reverse newlst))
		lst))

(format t "~A compressed is:~%~A~%" a (my-compr a))
(format t "~A compressed is:~%~A~%" '(1 0) (my-compr '(1 0)))
